<?php
/**
 * $Id: index.php v 1.00 21 June 2005 John N Exp $
 * Module: WF-Links
 * Version: v1.0.3
 * Release Date: 21 June 2005
 * Developer: John N
 * Team: WF-Projects
 * Licence: GNU
 */

include 'admin_header.php';
// global $xoopsModule, $xoopsModuleConfig, $wfmyts, $xoopsUser;
$mytree = new XoopsTree( $xoopsDB -> prefix( WFLINKS_CAT ), 'cid', 'pid' );

$op = wfl_cleanRequestVars( $_REQUEST, 'op', '' );
$lid = wfl_cleanRequestVars( $_REQUEST, 'lid', 0 );

function edit( $lid = 0 )
{
    global $xoopsDB, $wfmyts, $mytree, $imagearray;
    global $xoopsModuleConfig, $xoopsModule;

    $sql = 'SELECT * FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS ) . ' WHERE lid=' . $lid;
    if ( !$result = $xoopsDB -> query( $sql ) )
    {
        XoopsErrorHandler_HandleError( E_USER_WARNING, $sql, __FILE__, __LINE__ );
        return false;
    } 
    $link_array = $xoopsDB -> fetchArray( $xoopsDB -> query( $sql ) );

    $lid = $link_array['lid'] ? $link_array['lid'] : 0;
    $cid = $link_array['cid'] ? $link_array['cid'] : 0;
    $title = $link_array['title'] ? $wfmyts -> htmlSpecialCharsStrip( $link_array['title'] ) : '';
    $url = $link_array['url'] ? $wfmyts -> htmlSpecialCharsStrip( $link_array['url'] ) : 'http://';
    $publisher = $link_array['publisher'] ? $wfmyts -> htmlSpecialCharsStrip( $link_array['publisher'] ) : '';
    $screenshot = $link_array['screenshot'] ? $wfmyts -> htmlSpecialCharsStrip( $link_array['screenshot'] ) : '';
    $description = $link_array['description'] ? $wfmyts -> htmlSpecialCharsStrip( $link_array['description'] ) : '';
    $published = $link_array['published'] ? $link_array['published'] : 0;
    $expired = $link_array['expired'] ? $link_array['expired'] : 0;
    $updated = $link_array['updated'] ? $link_array['updated'] : 0;
    $offline = $link_array['offline'] ? $link_array['offline'] : 0;
    $forumid = $link_array['forumid'] ? $link_array['forumid'] : 0;
    $ipaddress = $link_array['ipaddress'] ? $link_array['ipaddress'] : 0;
    $notifypub = $link_array['notifypub'] ? $link_array['notifypub'] : 0;
    $urlrating = $link_array['urlrating'] ? $link_array['urlrating'] : 1;
    $directory = $xoopsModuleConfig['screenshots'];

    xoops_cp_header();
    wfl_adminmenu( _AM_WFL_MLINKS );

    $_vote_data = getVoteDetails( $lid );
    if ( $lid > 0 && $_vote_data['rate'] > 0 )
    {
        $_vote_data = getVoteDetails( $lid );
        $text_info = "
			<table width='100%'>
			 <tr>
			  <td width='50%' valign='top'>
			   <div><b>" . _AM_WFL_VOTE_TOTALRATE . ": </b>" . intval( $_vote_data['rate'] ) . "</div>
			   <div><b>" . _AM_WFL_VOTE_USERAVG . ": </b>" . intval( round( $_vote_data['avg_rate'], 2 ) ) . "</div>
			   <div><b>" . _AM_WFL_VOTE_MAXRATE . ": </b>" . intval( $_vote_data['min_rate'] ) . "</div>
			   <div><b>" . _AM_WFL_VOTE_MINRATE . ": </b>" . intval( $_vote_data['max_rate'] ) . "</div>
			  </td>
			  <td>		 
			   <div><b>" . _AM_WFL_VOTE_MOSTVOTEDTITLE . ": </b>" . intval( $_vote_data['max_title'] ) . "</div>
		          <div><b>" . _AM_WFL_VOTE_LEASTVOTEDTITLE . ": </b>" . intval( $_vote_data['min_title'] ) . "</div>
			   <div><b>" . _AM_WFL_VOTE_REGISTERED . ": </b>" . ( intval( $_vote_data['rate'] - $_vote_data['null_ratinguser'] ) ) . "</div>
			   <div><b>" . _AM_WFL_VOTE_NONREGISTERED . ": </b>" . intval( $_vote_data['null_ratinguser'] ) . "</div>
			  </td>
			 </tr>
			</table>";
        echo "
			<fieldset><legend style='font-weight: bold; color: #0A3760;'>" . _AM_WFL_VOTE_DISPLAYVOTES . "</legend>\n
			<div style='padding: 8px;'>$text_info</div>\n	
			<div style='padding: 8px;'><li>" . $imagearray['deleteimg'] . " " . _AM_WFL_VOTE_DELETEDSC . "</li></div>\n
			</fieldset>\n
			<br />\n";
    } 
    unset( $_vote_data );

    $caption = ( $lid ) ? _AM_WFL_LINK_MODIFYFILE : _AM_WFL_LINK_CREATENEWFILE;
    $sform = new XoopsThemeForm( $caption, "storyform", xoops_getenv( 'PHP_SELF' ) );
    $sform -> setExtra( 'enctype="multipart / form - data"' );
    if ( $lid )
    {
        $sform -> addElement( new XoopsFormLabel( _AM_WFL_LINK_ID, $lid ) );
        $sform -> addElement( new XoopsFormLabel( _AM_WFL_LINK_IP, $ipaddress ) );
    } 
    $sform -> addElement( new XoopsFormText( _AM_WFL_LINK_TITLE, 'title', 50, 255, $title ), true );
    $sform -> addElement( new XoopsFormText( _AM_WFL_LINK_DLURL, 'url', 50, 255, $url ), true );
    $sform -> addElement( new XoopsFormText( _AM_WFL_LINK_PUBLISHER, 'publisher', 50, 255, $publisher ), false );

    ob_start();
    $mytree -> makeMySelBox( 'title', 'title', $cid, 0 );
    $sform -> addElement( new XoopsFormLabel( _AM_WFL_LINK_CATEGORY, ob_get_contents() ) );
    ob_end_clean();

    $urlrating_array = wfl_rating_system();
    $urlrating_select = new XoopsFormSelect( _AM_WFL_LINK_DIRCA, 'urlrating', $urlrating );
    $urlrating_select -> addOptionArray( $urlrating_array );
    $sform -> addElement( $urlrating_select );

    $sform -> addElement( new XoopsFormDhtmlTextArea( _AM_WFL_LINK_DESCRIPTION, 'description', $description, 15, 60 ), true );
    $graph_array = &wflLists :: getListTypeAsArray( XOOPS_ROOT_PATH . "/" . $xoopsModuleConfig['screenshots'], $type = "images" );
    $indeximage_select = new XoopsFormSelect( '', 'screenshot', $screenshot );
    $indeximage_select -> addOptionArray( $graph_array );
    $indeximage_select -> setExtra( "onchange = 'showImgSelected(\"image\", \"screenshot\", \"" . $xoopsModuleConfig['screenshots'] . "\", \"\", \"" . XOOPS_URL . "\")'" );
    $indeximage_tray = new XoopsFormElementTray( _AM_WFL_LINK_SHOTIMAGE, '&nbsp;' );
    $indeximage_tray -> addElement( $indeximage_select );
    if ( !empty( $imgurl ) )
    {
        $indeximage_tray -> addElement( new XoopsFormLabel( '', " <br /><br />< img src='" . XOOPS_URL . "/" . $xoopsModuleConfig['screenshots'] . "/" . $screenshot . "' name = 'image' id = 'image' alt = '' / > " ) );
    } 
    else
    {
        $indeximage_tray -> addElement( new XoopsFormLabel( '', " <br /><br /><img src='" . XOOPS_URL . "/uploads/blank.gif' name='image' id='image' alt='' / > " ) );
    } 
    $sform -> addElement( $indeximage_tray );
    $sform -> insertBreak( sprintf( _AM_WFL_LINK_MUSTBEVALID, "<b>" . $directory . "</b>" ), "even" );

    ob_start();
    wflLists :: getforum( $xoopsModuleConfig['selectforum'], $forumid );
    $sform -> addElement( new XoopsFormLabel( _AM_WFL_LINK_DISCUSSINFORUM, ob_get_contents() ) );
    ob_end_clean();

    $publishtext = ( !$lid && !$published ) ? _AM_WFL_LINK_SETPUBLISHDATE : _AM_WFL_LINK_SETNEWPUBLISHDATE;
    if ( $published > time() )
    {
        $publishtext = _AM_WFL_LINK_SETPUBDATESETS;
    } 
    $ispublished = ( $published > time() ) ? 1 : 0 ;
    $publishdates = ( $published > time() ) ? _AM_WFL_LINK_PUBLISHDATESET . formatTimestamp( $published, $xoopsModuleConfig['dateformat'] ) : _AM_WFL_LINK_SETDATETIMEPUBLISH;
    $publishdate_checkbox = new XoopsFormCheckBox( '', 'publishdateactivate', $ispublished );
    $publishdate_checkbox -> addOption( 1, $publishdates . "<br /><br />" );

    if ( $lid )
    {
        $sform -> addElement( new XoopsFormHidden( 'was_published', $published ) );
        $sform -> addElement( new XoopsFormHidden( 'was_expired', $expired ) );
    } 

    $publishdate_tray = new XoopsFormElementTray( _AM_WFL_LINK_PUBLISHDATE, '' );
    $publishdate_tray -> addElement( $publishdate_checkbox );
    $publishdate_tray -> addElement( new XoopsFormDateTime( $publishtext, 'published', 15, $published ) );
    $publishdate_tray -> addElement( new XoopsFormRadioYN( _AM_WFL_LINK_CLEARPUBLISHDATE, 'clearpublish', 0, ' ' . _YES . '', ' ' . _NO . '' ) );
    $sform -> addElement( $publishdate_tray );

    $isexpired = ( $expired > time() ) ? 1: 0 ;
    $expiredates = ( $expired > time() ) ? _AM_WFL_LINK_EXPIREDATESET . formatTimestamp( $expired, $xoopsModuleConfig['dateformat'] ) : _AM_WFL_LINK_SETDATETIMEEXPIRE;
    $warning = ( $published > $expired && $expired > time() ) ? _AM_WFL_LINK_EXPIREWARNING : '';
    $expiredate_checkbox = new XoopsFormCheckBox( '', 'expiredateactivate', $isexpired );
    $expiredate_checkbox -> addOption( 1, $expiredates . " <br /> <br /> " );

    $expiredate_tray = new XoopsFormElementTray( _AM_WFL_LINK_EXPIREDATE . $warning, '' );
    $expiredate_tray -> addElement( $expiredate_checkbox );
    $expiredate_tray -> addElement( new XoopsFormDateTime( _AM_WFL_LINK_SETEXPIREDATE . " <br /> ", 'expired', 15, $expired ) );
    $expiredate_tray -> addElement( new XoopsFormRadioYN( _AM_WFL_LINK_CLEAREXPIREDATE, 'clearexpire', 0, ' ' . _YES . '', ' ' . _NO . '' ) );
    $sform -> addElement( $expiredate_tray );

    $linkstatus_radio = new XoopsFormRadioYN( _AM_WFL_LINK_FILESSTATUS, 'offline', $offline, ' ' . _YES . '', ' ' . _NO . '' );
    $sform -> addElement( $linkstatus_radio );

    $up_dated = ( $updated == 0 ) ? 0 : 1;
    $link_updated_radio = new XoopsFormRadioYN( _AM_WFL_LINK_SETASUPDATED, 'up_dated', $up_dated, ' ' . _YES . '', ' ' . _NO . '' );
    $sform -> addElement( $link_updated_radio );

    $result = $xoopsDB -> query( "SELECT COUNT( * ) FROM " . $xoopsDB -> prefix( WFLINKS_BROKEN ) . " WHERE lid = " . $lid );
    list ( $broken_count ) = $xoopsDB -> fetchRow( $result );
    if ( $broken_count > 0 )
    {
        $link_updated_radio = new XoopsFormRadioYN( _AM_WFL_LINK_DELEDITMESS, 'delbroken', 1, ' ' . _YES . '', ' ' . _NO . '' );
        $sform -> addElement( $editmess_radio );
    } 
    $sform -> insertBreak( _AM_WFL_LINK_CREATENEWSSTORY, "bg3" );
    $submitNews_radio = new XoopsFormRadioYN( _AM_WFL_LINK_SUBMITNEWS, 'submitnews', 0, ' ' . _YES . '', ' ' . _NO . '' );
    $sform -> addElement( $submitNews_radio );

    include_once XOOPS_ROOT_PATH . '/class/xoopstopic.php';
    $xt = new XoopsTopic( $xoopsDB -> prefix( 'topics' ) );
    ob_start();
    $xt -> makeTopicSelBox( 1, 0, "newstopicid" );
    $sform -> addElement( new XoopsFormLabel( _AM_WFL_LINK_NEWSCATEGORY, ob_get_contents() ) );
    ob_end_clean();

    $sform -> addElement( new XoopsFormText( _AM_WFL_LINK_NEWSTITLE, 'newsTitle', 50, 255, '' ), false );
    if ( $lid && $published == 0 )
    {
        $approved = ( $published == 0 ) ? 0 : 1;
        $approve_checkbox = new XoopsFormCheckBox( _AM_WFL_LINK_EDITAPPROVE, "approved", 1 );
        $approve_checkbox -> addOption( 1, " " );
        $sform -> addElement( $approve_checkbox );
    } 

    if ( !$lid )
    {
        $button_tray = new XoopsFormElementTray( '', '' );
        $button_tray -> addElement( new XoopsFormHidden( 'status', 1 ) );
        $button_tray -> addElement( new XoopsFormHidden( 'notifypub', $notifypub ) );
        $button_tray -> addElement( new XoopsFormHidden( 'op', 'save' ) );
        $button_tray -> addElement( new XoopsFormButton( '', '', _AM_WFL_BSAVE, 'submit' ) );
        $sform -> addElement( $button_tray );
    } 
    else
    {
        $button_tray = new XoopsFormElementTray( '', '' );
        $button_tray -> addElement( new XoopsFormHidden( 'lid', $lid ) );
        $button_tray -> addElement( new XoopsFormHidden( 'status', 2 ) );
        $hidden = new XoopsFormHidden( 'op', 'save' );
        $button_tray -> addElement( $hidden );

        $butt_dup = new XoopsFormButton( '', '', _AM_WFL_BMODIFY, 'submit' );
        $butt_dup -> setExtra( 'onclick="this . form . elements . op . value = \'save\'"' );
        $button_tray -> addElement( $butt_dup );
        $butt_dupct = new XoopsFormButton( '', '', _AM_WFL_BDELETE, 'submit' );
        $butt_dupct -> setExtra( 'onclick="this.form.elements.op.value=\'delete\'"' );
        $button_tray -> addElement( $butt_dupct );
        $butt_dupct2 = new XoopsFormButton( '', '', _AM_WFL_BCANCEL, 'submit' );
        $butt_dupct2 -> setExtra( 'onclick="this.form.elements.op.value=\'linksConfigMenu\'"' );
        $button_tray -> addElement( $butt_dupct2 );
        $sform -> addElement( $button_tray );
    } 
    $sform -> display();
    unset( $hidden ); 
    // }
    xoops_cp_footer();
} 

function fetchURL( $url = '', $timeout = 2 )
{
    $url = urldecode( $url );
    $url_parsed = parse_url( $url );
    if ( !isset( $url_parsed["host"] ) )
    {
        return '';
    } 

    $host = $url_parsed["host"];
    $host = ereg_replace( "http://", "", $host );
    $port = ( isset( $url_parsed["port"] ) ) ? $url_parsed["port"]: 80; 
    // Open the socket
    $handle = @fsockopen( 'http://' . $host, $port, $errno, $errstr, $timeout );
    if ( !$handle )
    {
        return null;
    } 
    else
    { 
        // Set read timeout
        stream_set_timeout( $handle, $timeout );
        for( $i = 0;$i < 1;$i++ )
        { 
            // Time the responce
            list( $usec, $sec ) = explode( " ", microtime( true ) );
            $start = ( float )$usec + ( float )$sec; 
            // send somthing
            $write = fwrite( $handle, "return ping\n" );
            if ( !$write )
            {
                return '';
            } 
            fread( $handle, 1024 ); 
            // Work out if we got a responce and time it
            list( $usec, $sec ) = explode( " ", microtime( true ) );
            $laptime = ( ( float )$usec + ( float )$sec ) - $start;
            if ( $laptime > $timeout )
            {
                return 'No Reply';
            } 
            else
            {
                return round( $laptime, 3 );
            } 
        } 
        fclose( $handle );
    } 
} 

switch ( strtolower( $op ) )
{
    case "pingtime";
    case "is_broken";

        $_type = ( $op == "pingtime" ) ? "is_broken" : "pingtime";

        $start = wfl_cleanRequestVars( $_REQUEST, 'start', 0 );
        $ping = wfl_cleanRequestVars( $_REQUEST, 'ping', 0 );
        $cid = wfl_cleanRequestVars( $_REQUEST, 'cid', 0 );

        $sql = 'SELECT * FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS );
        if ( $cid > 0 )
        {
            $sql .= ' WHERE cid=' . $cid;
        } 
        $sql .= ' ORDER BY lid DESC';
        if ( !$result = $xoopsDB -> query( $sql ) )
        {
            XoopsErrorHandler_HandleError( E_USER_WARNING, $sql, __FILE__, __LINE__ );
            return false;
        } 
        $broken_array = $xoopsDB -> query( $sql, $xoopsModuleConfig['admin_perpage'], $start );
        $broken_array_count = $xoopsDB -> getRowsNum( $result );

        $heading = ( $op == "pingtime" ) ? _AM_WFL_PINGTIMES : _AM_WFL_LISTBROKEN;

        xoops_cp_header();
        wfl_adminmenu( _AM_WFL_BINDEX, '', $heading );
        echo "
			<table width='100%' cellspacing='1' cellpadding='2' border='0' class='outer'>\n
			<tr>\n
			<th style='text-align: center;'>" . _AM_WFL_MINDEX_ID . "</th>\n
			<th><b>" . _AM_WFL_MINDEX_TITLE . "</th>\n
			<th style='text-align: center;'>"._AM_WFL_MINDEX_POSTER."</th>\n
			<th style='text-align: center;'>" . _AM_WFL_MINDEX_PUBLISHED . "</th>\n
	        <th style='text-align: center;'>" . _AM_WFL_MINDEX_RESPONSE . "</th>\n
	        <th style='text-align: center;'>" . _AM_WFL_MINDEX_ACTION . "</th>\n
			</tr>\n
		";

        if ( $broken_array_count > 0 )
        {
            while ( $published = $xoopsDB -> fetchArray( $broken_array ) )
            {
                $_ping_results = fetchURL( $published['url'] );

                if ( !$_ping_results )
                {
                    $_ping_results = 'No Response';
                } 
                else
                {
                    $_ping_results = $_ping_results . '(s)';
                } 

                $lid = $published['lid'];
                $cid = $published['cid'];
                $title = "<a href='../singlelink.php?cid=" . $published['cid'] . "&amp;lid=" . $published['lid'] . "'>" . $wfmyts -> htmlSpecialCharsStrip( trim( $published['title'] ) ) . "</a>";;
                $maintitle = urlencode( $wfmyts -> htmlSpecialChars( trim( $published['title'] ) ) );
                $submitter = xoops_getLinkedUnameFromId( $published['submitter'] );
                $publish = formatTimestamp( $published['published'], $xoopsModuleConfig['dateformat'] );
                $status = ( $published['published'] > 0 ) ? $imagearray['online'] : "<a href='newlinks.php'>" . $imagearray['offline'] . "</a>";
                $icon = "<a href='index.php?op=edit&amp;lid=" . $lid . "'>" . $imagearray['editimg'] . "</a>";
                $icon .= "<a href='index.php?op=delete&amp;lid=" . $lid . "'>" . $imagearray['deleteimg'] . "</a>";
                echo "<tr style='text-align: center;'>\n
						<td class='head'>" . $lid . "</td>\n
						<td class='even' style='text-align: left;'>" . $title . "</td>\n
						<td class='even'>" . $submitter . "</td>\n
						<td class='even'>" . $publish . "</td>\n
						<td class='even'>" . $_ping_results . "</td>\n
						<td class='even' width='10%'>$icon</td>\n
						</tr>\n";
                unset( $published );
            } 
        } 
        else
        {
            wfl_linklistfooter();
        } 
        wfl_linklistpagenav( $broken_array_count, $start, 'art', 'op=' . $op );
        xoops_cp_footer();
        break;

    case "edit":
        edit( $lid );
        break;

    case "save":
        $groups = isset( $_POST['groups'] ) ? $_POST['groups'] : array();
        $lid = ( !empty( $_POST['lid'] ) ) ? $_POST['lid'] : 0;
        $cid = ( !empty( $_POST['cid'] ) ) ? $_POST['cid'] : 0;
        $urlrating = ( !empty( $_POST['urlrating'] ) ) ? $_POST['urlrating'] : 6;
        $status = ( !empty( $_POST['status'] ) ) ? $_POST['status'] : 2;
        $url = ( $_POST["url"] != "http://" ) ? $wfmyts -> addslashes( $_POST["url"] ) : '';
        $title = $wfmyts -> addslashes( trim( $_POST["title"] ) );
        /**
         * Get data from form
         */
        $screenshot = ( $_POST["screenshot"] != "blank.gif" ) ? $wfmyts -> addslashes( $_POST["screenshot"] ) : '';
        $description = $wfmyts -> addslashes( trim( $_POST["description"] ) );
        $submitter = $xoopsUser -> uid();
        $publisher = $xoopsUser -> uname();
        $forumid = ( isset( $_POST["forumid"] ) && $_POST["forumid"] > 0 ) ? intval( $_POST["forumid"] ) : 0;
        $updated = ( isset( $_POST['was_published'] ) && $_POST['was_published'] == 0 ) ? 0 : time();
        if ( $_POST['up_dated'] == 0 )
        {
            $updated = 0;
            $status = 1;
        } 
        $offline = ( $_POST['offline'] == 1 ) ? 1 : 0;
        $approved = ( isset( $_POST['approved'] ) && $_POST['approved'] == 1 ) ? 1 : 0;
        $notifypub = ( isset( $_POST['notifypub'] ) && $_POST['notifypub'] == 1 );
        if ( !$lid )
        {
            $date = time();
            $publishdate = time();
        } 
        else
        {
            $publishdate = $_POST['was_published'];
            $expiredate = $_POST['was_expired'];
        } 

        if ( $approved == 1 && empty( $publishdate ) )
        {
            $publishdate = time();
        } 

        if ( isset( $_POST['publishdateactivate'] ) )
        {
            $publishdate = strtotime( $_POST['published']['date'] ) + $_POST['published']['time'];
        } 
        if ( $_POST['clearpublish'] )
        {
            $result = $xoopsDB -> query( "SELECT date FROM " . $xoopsDB -> prefix( WFLINKS_LINKS ) . " WHERE lid=" . $lid );
            list( $date ) = $xoopsDB -> fetchRow( $result );
            $publishdate = $date;
        } 

        if ( isset( $_POST['expiredateactivate'] ) )
        {
            $expiredate = strtotime( $_POST['expired']['date'] ) + $_POST['expired']['time'];
        } 
        if ( $_POST['clearexpire'] )
        {
            $expiredate = '0';
        } 
        /**
         * Update or insert linkload data into database
         */
        if ( !$lid )
        {
            $date = time();
            $publishdate = time();
            $ipaddress = $_SERVER['REMOTE_ADDR'];
            $sql = "INSERT INTO " . $xoopsDB -> prefix( WFLINKS_LINKS ) . " (lid, cid, title, url, screenshot, submitter, publisher, status, date, hits, rating, votes, comments, forumid, published, expired, updated, offline, description, ipaddress, notifypub, urlrating )";
            $sql .= " VALUES 	('', $cid, '$title', '$url', '$screenshot', '$submitter', '$publisher','$status', '$date', 0, 0, 0, 0, '$forumid', '$publishdate', 	0, '$updated', '$offline', '$description', '$ipaddress', '0', '$urlrating')";
            $newid = $xoopsDB -> getInsertId();
        } 
        else
        {
            $sql = "UPDATE " . $xoopsDB -> prefix( WFLINKS_LINKS ) . " SET cid = $cid, title='$title', url='$url', screenshot='$screenshot', publisher='$publisher', status='$status', forumid='$forumid', published='$publishdate', expired='$expiredate', updated='$updated', offline='$offline', description='$description', urlrating='$urlrating' WHERE lid = " . $lid;
        } 
        if ( !$result = $xoopsDB -> queryF( $sql ) )
        {
            XoopsErrorHandler_HandleError( E_USER_WARNING, $sql, __FILE__, __LINE__ );
            return false;
        } 
        /**
         * Send notifications
         */
        if ( !$lid )
        {
            $tags = array();
            $tags['LINK_NAME'] = $title;
            $tags['LINK_URL'] = XOOPS_URL . '/modules/' . $xoopsModule -> getVar( 'dirname' ) . '/singlelink.php?cid=' . $cid . '&amp;lid=' . $newid;
            $sql = "SELECT title FROM " . $xoopsDB -> prefix( WFLINKS_CAT ) . " WHERE cid=" . $cid;
            $result = $xoopsDB -> query( $sql );
            $row = $xoopsDB -> fetchArray( $xoopsDB -> query( $sql ) );
            $tags['CATEGORY_NAME'] = $row['title'];
            $tags['CATEGORY_URL'] = XOOPS_URL . '/modules/' . $xoopsModule -> getVar( 'dirname' ) . '/viewcat.php?cid=' . $cid;
            $notification_handler = &xoops_gethandler( 'notification' );
            $notification_handler -> triggerEvent( 'global', 0, 'new_link', $tags );
            $notification_handler -> triggerEvent( 'category', $cid, 'new_link', $tags );
        } 
        if ( $lid && $approved && $notifypub )
        {
            $tags = array();
            $tags['LINK_NAME'] = $title;
            $tags['LINK_URL'] = XOOPS_URL . '/modules/' . $xoopsModule -> getVar( 'dirname' ) . '/singlelink.php?cid=' . $cid . '&amp;lid=' . $lid;
            $sql = 'SELECT title FROM ' . $xoopsDB -> prefix( 'mylinks_cat' ) . ' WHERE cid=' . $cid;
            $result = $xoopsDB -> query( $sql );
            $row = $xoopsDB -> fetchArray( $result );
            $tags['CATEGORY_NAME'] = $row['title'];
            $tags['CATEGORY_URL'] = XOOPS_URL . '/modules/' . $xoopsModule -> getVar( 'dirname' ) . '/viewcat.php?cid=' . $cid;
            $notification_handler = &xoops_gethandler( 'notification' );
            $notification_handler -> triggerEvent( 'global', 0, 'new_link', $tags );
            $notification_handler -> triggerEvent( 'category', $cid, 'new_link', $tags );
            $notification_handler -> triggerEvent( 'link', $lid, 'approve', $tags );
        } 
        $message = ( !$lid ) ? _AM_WFL_LINK_NEWFILEUPLOAD : _AM_WFL_LINK_FILEMODIFIEDUPDATE ;
        $message = ( $lid && !$_POST['was_published'] && $approved ) ? _AM_WFL_LINK_FILEAPPROVED : $message;
        if ( wfl_cleanRequestVars( $_REQUEST, 'delbroken', 0 ) )
        {
            $sql = 'DELETE FROM ' . $xoopsDB -> prefix( WFLINKS_BROKEN ) . ' WHERE lid =' . $lid;
            if ( !$result = $xoopsDB -> queryF( $sql ) )
            {
                XoopsErrorHandler_HandleError( E_USER_WARNING, $sql, __FILE__, __LINE__ );
                return false;
            } 
        } 
        if ( wfl_cleanRequestVars( $_REQUEST, 'submitnews', 0 ) )
        {
            include_once "newstory.php";
        } 
        redirect_header( "index.php", 1, $message );
        break;

    case "delete":
        if ( wfl_cleanRequestVars( $_REQUEST, 'confirm', 0 ) )
        {
            $title = wfl_cleanRequestVars( $_REQUEST, 'title', 0 );
            $sql = 'DELETE FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS ) . ' WHERE lid=' . $lid;
            if ( !$result = $xoopsDB -> query( $sql ) )
            {
                XoopsErrorHandler_HandleError( E_USER_WARNING, $sql, __FILE__, __LINE__ );
                return false;
            } 
            $sql = 'DELETE FROM ' . $xoopsDB -> prefix( WFLINKS_VOTEDATA ) . ' WHERE lid=' . $lid;
            if ( !$result = $xoopsDB -> query( $sql ) )
            {
                XoopsErrorHandler_HandleError( E_USER_WARNING, $sql, __FILE__, __LINE__ );
                return false;
            } 
            // delete comments
            xoops_comment_delete( $xoopsModule -> getVar( 'mid' ), $lid );
            redirect_header( "index.php", 1, sprintf( _AM_WFL_LINK_FILEWASDELETED, $title ) );
            exit();
        } 
        else
        {
            $sql = 'SELECT lid, title, url FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS ) . ' WHERE lid = ' . $lid;
            if ( !$result = $xoopsDB -> query( $sql ) )
            {
                XoopsErrorHandler_HandleError( E_USER_WARNING, $sql, __FILE__, __LINE__ );
                return false;
            } 			
			list( $lid, $title ) = $xoopsDB -> fetchrow( $result );
           
		    xoops_cp_header();
            wfl_adminmenu( _AM_WFL_BINDEX );
            xoops_confirm( array( 'op' => 'delete', 'lid' => $lid, 'confirm' => 1, 'title' => $title ), 'index.php', _AM_WFL_LINK_REALLYDELETEDTHIS . "<br /><br>" . $title, _DELETE );
            xoops_cp_footer();
        } 
        break;

    case "delvote":
        $rid = wfl_cleanRequestVars( $_REQUEST, 'rid', 0 );
        $sql = 'DELETE FROM ' . $xoopsDB -> prefix( WFLINKS_VOTEDATA ) . ' WHERE ratingid=' . $rid;
        if ( !$result = $xoopsDB -> queryF( $sql ) )
        {
            XoopsErrorHandler_HandleError( E_USER_WARNING, $sql, __FILE__, __LINE__ );
            return false;
        } 
        wfl_updaterating( $rid );
        redirect_header( "index.php", 1, _AM_WFL_VOTE_VOTEDELETED );
        break;

    case 'main':
    default:

        $start = wfl_cleanRequestVars( $_REQUEST, 'rid', 0 );
        $start1 = wfl_cleanRequestVars( $_REQUEST, 'start1', 0 );
        $start2 = wfl_cleanRequestVars( $_REQUEST, 'start2', 0 );
        $start3 = wfl_cleanRequestVars( $_REQUEST, 'start3', 0 );
        $start4 = wfl_cleanRequestVars( $_REQUEST, 'start4', 0 );
#        $start5 = wfl_cleanRequestVars( $_REQUEST, 'start5', 0 );
        $totalcats = wfl_totalcategory();

        $result = $xoopsDB -> query( 'SELECT COUNT(*) FROM ' . $xoopsDB -> prefix( WFLINKS_BROKEN ) );
        list( $totalbrokenlinks ) = $xoopsDB -> fetchRow( $result );
        $result2 = $xoopsDB -> query( 'SELECT COUNT(*) FROM ' . $xoopsDB -> prefix( WFLINKS_MOD ) );
        list( $totalmodrequests ) = $xoopsDB -> fetchRow( $result2 );
        $result3 = $xoopsDB -> query( 'SELECT COUNT(*) FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS ) . ' WHERE published = 0' );
        list( $totalnewlinks ) = $xoopsDB -> fetchRow( $result3 );
        $result4 = $xoopsDB -> query( 'SELECT COUNT(*) FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS ) . ' WHERE published > 0' );
        list( $totallinks ) = $xoopsDB -> fetchRow( $result4 );

        xoops_cp_header();
        wfl_adminmenu( _AM_WFL_BINDEX );
        echo "
			<fieldset><legend style='font-weight: bold; color: #0A3760;'>" . _AM_WFL_MINDEX_LINKSUMMARY . "</legend>\n
			<div style='padding: 8px;'><small>\n
			<a href='category.php'>" . _AM_WFL_SCATEGORY . "</a><b>" . $totalcats . "</b> | \n
			<a href='index.php'>" . _AM_WFL_SFILES . "</a><b>" . $totallinks . "</b> | \n
			<a href='newlinks.php'>" . _AM_WFL_SNEWFILESVAL . "</a><b>" . $totalnewlinks . "</b> | \n
			<a href='modifications.php'>" . _AM_WFL_SMODREQUEST . "</a><b>" . $totalmodrequests . "</b> | \n
			<a href='brokenlink.php'>" . _AM_WFL_SBROKENSUBMIT . "</a><b>" . $totalbrokenlinks . "</b>\n
			</small></div></fieldset><br />\n
		";

        if ( $totalcats > 0 )
        {
            $sform = new XoopsThemeForm( _AM_WFL_CCATEGORY_MODIFY, "category", "category.php" );
            ob_start();
            $mytree -> makeMySelBox( "title", "title" );
            $sform -> addElement( new XoopsFormLabel( _AM_WFL_CCATEGORY_MODIFY_TITLE, ob_get_contents() ) );
            ob_end_clean();
            $dup_tray = new XoopsFormElementTray( '', '' );
            $dup_tray -> addElement( new XoopsFormHidden( 'op', 'modCat' ) );
            $butt_dup = new XoopsFormButton( '', '', _AM_WFL_BMODIFY, 'submit' );
            $butt_dup -> setExtra( 'onclick="this.form.elements.op.value=\'modCat\'"' );
            $dup_tray -> addElement( $butt_dup );
            $butt_dupct = new XoopsFormButton( '', '', _AM_WFL_BDELETE, 'submit' );
            $butt_dupct -> setExtra( 'onclick="this.form.elements.op.value=\'del\'"' );
            $dup_tray -> addElement( $butt_dupct );
            $sform -> addElement( $dup_tray );
            $sform -> display();
        } 

        if ( $totallinks > 0 )
        {
            $sql = 'SELECT * FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS ) . ' WHERE published > 0  ORDER BY lid DESC' ;
            $published_array = $xoopsDB -> query( $sql, $xoopsModuleConfig['admin_perpage'], $start );
            $published_array_count = $xoopsDB -> getRowsNum( $xoopsDB -> query( $sql ) );
            wfl_linklistheader( _AM_WFL_MINDEX_PUBLISHEDLINK );
            if ( $published_array_count > 0 )
            {
                while ( $published = $xoopsDB -> fetchArray( $published_array ) )
                {
                    wfl_linklistbody( $published );
                } 
            } 
            else
            {
                wfl_linklistfooter();
            } 
            wfl_linklistpagenav( $published_array_count, $start, 'art' );
            
			/**
             * Auto Publish
             */
#            $sql = 'SELECT * FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS ) . '	WHERE published > ' . time() . ' ORDER BY lid DESC' ;
#            $auto_publish_array = $xoopsDB -> query( $sql, $xoopsModuleConfig['admin_perpage'], $start2 );
#            $auto_publish_count = $xoopsDB -> getRowsNum( $xoopsDB -> query( $sql ) );
#            wfl_linklistheader( _AM_WFL_MINDEX_AUTOPUBLISHEDLINK );
#            if ( $auto_publish_count > 0 )
#            {
#                while ( $auto_publish = $xoopsDB -> fetchArray( $auto_publish_array ) )
#                {
#                    wfl_linklistbody( $auto_publish );
#                } 
#            } 
#            else
#            {
#                wfl_linklistfooter();
#            } 
#            wfl_linklistpagenav( $auto_publish_count, $start2, 'art2' );
#            /**
#             * Auto expire
#             */
#            $sql = 'SELECT * FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS ) . ' WHERE expired > ' . time() . ' ORDER BY lid DESC' ;
#            $auto_expire_array = $xoopsDB -> query( $sql, $xoopsModuleConfig['admin_perpage'], $start3 );
#            $auto_expire_count = $xoopsDB -> getRowsNum( $xoopsDB -> query( $sql ) );
#            wfl_linklistheader( _AM_WFL_MINDEX_AUTOEXPIRE );
#            if ( $auto_expire_count > 0 )
#            {
#                while ( $auto_expire = $xoopsDB -> fetchArray( $auto_expire_array ) )
#                {
#                    wfl_linklistbody( $auto_expire );
#                } 
#            } 
#            else
#            {
#                wfl_linklistfooter();
#            } 
#            wfl_linklistpagenav( $auto_expire_count, $start3, 'art3' );
#            
#			/**
#             * Expired
#             */
#            $sql = 'SELECT * FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS ) . ' WHERE expired > 0 AND expired < ' . time() . '  ORDER BY lid DESC' ;
#            $expired_array = $xoopsDB -> query( $sql, $xoopsModuleConfig['admin_perpage'], $start4 );
#            $expired_count = $xoopsDB -> getRowsNum( $xoopsDB -> query( $sql ) );
#            wfl_linklistheader( _AM_WFL_MINDEX_EXPIRED );
#            if ( $expired_count > 0 )
#            {
#                while ( $expired = $xoopsDB -> fetchArray( $expired_array ) )
#                {
#                    wfl_linklistbody( $expired );
#                } 
#            } 
#            else
#            {
#                wfl_linklistfooter();
#            } 
#            wfl_linklistpagenav( $expired_count, $start4, 'art4' );
#			
#            /**
#             * Offline
#             */
#            $sql = 'SELECT * FROM ' . $xoopsDB -> prefix( WFLINKS_LINKS ) . ' WHERE offline = 1 ORDER BY lid DESC' ;
#            $offline_array = $xoopsDB -> query( $sql, $xoopsModuleConfig['admin_perpage'], $start5 );
#            $offline_count = $xoopsDB -> getRowsNum( $xoopsDB -> query( $sql ) );
#            wfl_linklistheader( _AM_WFL_MINDEX_OFFLINELINK );
#            if ( $offline_count > 0 )
#            {
#                while ( $is_offline = $xoopsDB -> fetchArray( $offline_array ) )
#                {
#                    wfl_linklistbody( $is_offline );
#                } 
#            } 
#            else
#            {
#                wfl_linklistfooter();
#            } 
#            wfl_linklistpagenav( $offline_count, $start5, 'art5' );
        } 
        xoops_cp_footer();
        break;
} 

?>